package cc.mrbird.febs.activity.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuwenze.poi.ExcelKit;

import cc.mrbird.febs.activity.entity.Activity;
import cc.mrbird.febs.activity.service.IActivityService;
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import lombok.RequiredArgsConstructor;

/**
 * 活动信息表 Controller
 *
 * @author Lord
 * @date 2020-06-06 16:11:26
 */
@Validated
@Controller
@RequiredArgsConstructor
public class ActivityController extends BaseController {

    private final IActivityService activityService;

    @GetMapping("activity")
    @ResponseBody
    @RequiresPermissions("activity:list")
    public FebsResponse getAllActivitys(Activity activity) {
        return new FebsResponse().success().data(activityService.findActivitys(activity));
    }

    @GetMapping("activity/list")
    @ResponseBody
    @RequiresPermissions("activity:list")
    public FebsResponse activityList(QueryRequest request, Activity activity) {
        Map<String, Object> dataTable = getDataTable(this.activityService.findActivitys(request, activity));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Activity", exceptionMessage = "新增Activity失败")
    @PostMapping("activity")
    @ResponseBody
    @RequiresPermissions("activity:add")
    public FebsResponse addActivity(@Valid Activity activity) {
        this.activityService.createActivity(activity);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Activity", exceptionMessage = "删除Activity失败")
    @GetMapping("activity/delete")
    @ResponseBody
    @RequiresPermissions("activity:delete")
    public FebsResponse deleteActivity(Activity activity) {
        this.activityService.deleteActivity(activity);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Activity", exceptionMessage = "修改Activity失败")
    @PostMapping("activity/update")
    @ResponseBody
    @RequiresPermissions("activity:update")
    public FebsResponse updateActivity(Activity activity) {
        this.activityService.updateActivity(activity);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Activity", exceptionMessage = "导出Excel失败")
    @PostMapping("activity/excel")
    @ResponseBody
    @RequiresPermissions("activity:export")
    public void export(QueryRequest queryRequest, Activity activity, HttpServletResponse response) {
        List<Activity> activitys = this.activityService.findActivitys(queryRequest, activity).getRecords();
        ExcelKit.$Export(Activity.class, response).downXlsx(activitys, false);
    }
}
